<?php
namespace app\admin\controller;
use think\Controller;
class Admin extends Common
{   
	//后台管理用户列表显示
	public function index(){
		$all = input('get.all');
		$data = model('Admin')->index();
		$this->assign('all',$all);
		$this->assign('data',$data);
		return $this->fetch();
	}

	//管理员个人信息
	public function see(){
		$admin_id = session('admin_id');
		$data = db('admin')->where('admin_id',$admin_id)->find();
		$this->assign('data',$data);
		return $this->fetch();
	}

	//后台管理员个人信息
	public function seelist(){
		$admin_id = input('param.admin_id');
		$data = db('admin')->where('admin_id',$admin_id)->find();
		$this->assign('data',$data);
		return $this->fetch('admin/see');
	}

	//添加管理员
	public function add(){
		return $this->fetch();
	}

	//添加后台管理用户
	public function adds(){
	    if(request() -> isPost()){
	    	$add = model('Admin')->add();
	    	if($add['code']){
                $this->error($add['msg'],'admin/admin/index','',1);
	    	}else{
	    		$this->error($add['msg'],'','',1);
	    	}

	    }else{
	    	$this->error('请求失败');
		     exit;
	    }
	}
    
    //管理员用户信息修改
	public function edit(){
		$admin_id = input('param.admin_id');
		$data = db('admin')->field('user_name')->where('admin_id',$admin_id)->find();
		$this->assign('data',$data);
		return $this->fetch();
	}

	//管理员用户信息修改
	public function edits(){
		if(request()->isPost()){
			$edit = model('Admin')->edit();
			if($edit['valid']){
                   $this->success($edit['msg'],'admin/admin/index','',1);
                    exit;
                }else{
                    $this->error($edit['msg'],'','',1);
                    exit;
            }
		}else{
			$this->error('请求失败');
		}
	}	

	//删除单条记录(删除数据)
	public function del(){
		$admin_id = input('param.admin_id');
		if(session('admin_id') == 1){
			$del = db('admin')->where('admin_id',$admin_id)->delete();
			if($del > 0){
				$content = '删除管理员列表';
	        	admin_log($content); 
				$this->success('删除成功','index','',1);
			}else{
				$this->error('删除失败','','',1);
			}
		}else{
			$this->error('您没有删除管理员的权限!','index','',1);
		}
		
	}

	//批量删除(批量删除数据)
	public function deleteall(){
		if(session('admin_id') == 1){
			if(request() -> isAjax()){
			$admin_id=$_POST['admin_id'];
			if(db('admin')->delete($admin_id)){
				$content = '删除管理员列表';
        	    admin_log($content); 
				echo json_encode(1);
			}else{
				echo json_encode(2);
			}
		  }
		}else{
           $this->error('您没有删除管理员的权限!','index','',1);
		}
	}
}